//
//  titlexqViewController.m
//  hainingOASystem
//
//  Created by michael on 2017/11/21.
//  Copyright © 2017年 czd. All rights reserved.
//

#import "titlexqViewController.h"

@interface titlexqViewController ()

@end

@implementation titlexqViewController
- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = self.view.bounds;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"详情";
          self.view.backgroundColor = [UIColor whiteColor];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];

[self requestdata];
    // Do any additional setup after loading the view.
}

- (void)requestdata{


NSString *string = [[NSString alloc]initWithFormat:@"%@%@",knewsxq,_id];
[httpTool ZBPostNetDataWith:string withDic:nil andSuccess:^(NSDictionary* dictionary){
NSLog(@"%@",dictionary);
 
_dic = dictionary;

 [self addHeaderUI];
        [self loadWebViewURL];
        if (!dictionary) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
 }andFailure:^{

 }];


}
- (void)addHeaderUI{

    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:headerView];
    self.headerView = headerView;
          CGFloat titleSize = [OAToolBox sizebyText:_dic[@"title"] font:18 minsWidth:20];

    UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(10, 10, KScreenWidth, titleSize);
label1.text = _dic[@"title"];
label1.numberOfLines = 0;
label1.textColor = [UIColor blackColor];
     label1.font = [UIFont boldSystemFontOfSize:18];
    [headerView addSubview:label1];
    
UILabel *label2 = [[UILabel alloc]init];

label2.frame = CGRectMake(label1.left, label1.bottom+10,(KScreenWidth-20)/2, 30);
label2.text = [NSString stringWithFormat:@"信息来源:%@",_dic[@"CopyFrom"]];
label2.textColor = [UIColor blackColor];
label2.numberOfLines = 0;
     label2.font = [UIFont systemFontOfSize:12];
    [headerView addSubview:label2];
    UILabel *label3 = [[UILabel alloc]init];
    
label3.frame = CGRectMake(label2.right,label2.top, KScreenWidth-20-label2.width, 30);
label3.numberOfLines = 0;
label3.text = [NSString stringWithFormat:@"发布时间:%@",_dic[@"updateTime"]];
label3.textColor = [UIColor blackColor];
label3.textAlignment = NSTextAlignmentRight;
     label3.font = [UIFont systemFontOfSize:12];
    [headerView addSubview:label3];
    
    UILabel *label4 = [[UILabel alloc]init];
label4.frame = CGRectMake(label1.left, label2.bottom,label1.width, 20);
label4.text = [NSString stringWithFormat:@"作者:%@",_dic[@"Author"]];
label4.textColor = [UIColor blackColor];
     label4.font = [UIFont systemFontOfSize:12];
    [headerView addSubview:label4];
   
  
     headerView.frame = CGRectMake(0, 0, KScreenWidth, CGRectGetMaxY(label4.frame));
    
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(headerView.frame) + KScreenHeight );
    
}
/**
 *  加载网页
 */
- (void)loadWebViewURL
{
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    self.hud = hud;
    
    NSString *urlStr = _dic[@"Content"];

    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    UIWebView *webView = [[UIWebView alloc]init];
    webView.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
    
    self.webView = webView;
    
    //bounces
    webView.scrollView.bounces = NO;
    //禁止webView滚动
    webView.scrollView.scrollEnabled = NO;
    
    webView.delegate = self;
//    webView.scalesPageToFit = YES;
    
    [webView loadHTMLString:urlStr baseURL:nil];
    
    [self.scrollView addSubview:webView];
    
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//    CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
//    //把webView的高度设置成网页内容的高度
//    self.webView.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), self.scrollView.bounds.size.width, height);
//    //设置scrollView的滚动范围
//    self.scrollView.contentSize = CGSizeMake(0, height+CGRectGetMaxY(self.headerView.frame));
[MBProgressHUD hideHUDForView:self.view animated:YES];

    CGSize actualSize = [webView sizeThatFits:CGSizeZero];
    CGRect newFrame = webView.frame;
    newFrame.size.height = actualSize.height;
    webView.frame = newFrame;
     self.scrollView.contentSize = CGSizeMake(0,  newFrame.size.height+CGRectGetMaxY(self.headerView.frame));
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

